/**
 *去除字符串前后空格
 *给String对象原型添加trim方法
 */
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

/**
 *提交方法校验输入是否正确
 */
function mySubmit() {
    if (checkForm()) {
        document.forms[0].submit();
    }
}

/**
 *校验表单
 */
function checkForm(type) {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    if (type == "username") {
        var username_tips = document.getElementById("username_tips");
        if (username.trim() == "") {
            username_tips.innerHTML = "<font color='red'>用户名不能为空！</font>";
        } else {
            username_tips.innerHTML = "";
        }
    } else if (type == "password") {
        var password_tips = document.getElementById("password_tips");
        if (password.trim() == "") {
            password_tips.innerHTML = "<font color='red'>密码不能为空！</font>";
        } else {
            password_tips.innerHTML = "";
        }
    } else {
        var username_tips = document.getElementById("username_tips");
        var password_tips = document.getElementById("password_tips");
        if (username.trim() != "" && password.trim() != "") {
            username_tips.innerHTML = "";
            password_tips.innerHTML = "";
            return true;
        } else {
            if (username.trim() == "") {
                username_tips.innerHTML = "<font color='red'>用户名不能为空！</font>";
            }
            if (password.trim() == "") {
                password_tips.innerHTML = "<font color='red'>密码不能为空！</font>";
            }
            return false;
        }
    }
}
